<?php
/**
 * scottlib-cmd
 * CLI for scottlib
 * @author Aaron Collegeman aaroncollegeman.com
 * @version 1.0.0 (beta): $Id: scottlib-cmd.php 156 2009-01-12 00:43:50Z acollegeman $
 *
 * Copyright (C)2008 Collegeman.net, LLC.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 
 */

@include_once('scottlib-cfg.php');

$session = array();
$next_property = null;

function chomp($input) {
	return preg_replace("/\n$/", "", $input);	
}

function cmd($input) {
	preg_match("/(\w+)\b/", $input, $matches);
	return $matches[1];	
}

function println($out = null) {
	echo "$out\n";	
}

function param($input, $index, $all = false) {
	$params = split(" ", chomp($input));
	if (!$all) 
		return (isset($params[$index]) ? $params[$index] : null);
	else {
		array_shift($params);
		return join(" ", $params);	
	}
}	

function slvalue($input) {
	if (preg_match("/^(md5|date|number)\(.*\)$/", $input, $matches)) {
		$value = $matches[2];
		if ($matches[1] == 'md5')
			return md5($value);
		else if ($matches[1] == 'date')
			return sldate($value);
		else if ($matches[1] == 'number')
			return slnumberencode($value);	
	}
	else
		return $input;
}

do {
	
	if ($next_property == null && $input) {
		$cmd = cmd($input);
		
		if (in_array($cmd, array("quit", "exit"))) {
			println("Goodbye!");
			exit(0);
		}
		
		else if ($cmd == 'connect') {
			if ($connectionString = param($input, 1)) {
				if (preg_match("/([^:]+):([^@]+)@(.+)/", $connectionString, $matches)) {
					$session['connect.host'] = $matches[3];
					$session['connect.user'] = $matches[1];
					$session['connect.pass'] = $matches[2];
					$next_property = 'connect.database';
				}
				else
					println('Connection string must be username:password@host');
			}
			else
				$next_property = 'connect.host';
		}
		
		else if ($cmd == 'query') {
			if (!($query = param($input, 1, true))) {
				println("You didn't ask me to query anything.");
			}
			else {
				try {
					$records = query($query);
					
				} catch (Exception $e) {
					println($e->getMessage());	
				}
			}
		}
		
		else if ($cmd == 'execute' || $cmd == 'x') {
			if (!($query = param($input, 1, true))) {
				println("You didn't ask me to execute anything.");
			}
			else {
				try {
					$affected = execute($query);
					echo "$affected records affected.";
				} catch (Exception $e) {
					println($e->getMessage());	
				}
			}
		}
		
		else if ($cmd == 'createDomain') {
			if (!($domain = param($input, 1))) {
				println("Syntax: createDomain <domain>[@<system>]");
			}
			else {
				if (preg_match("/([^@]+)@(.*)/", $domain, $matches)) {
					$domain = $matches[1];
					$system = $matches[2];	
				}
				else
					$system = 'default';
			
				try {
					slData::createDomain($domain, $system);		
				} catch (Exception $e) {
					println($e->getMessage());
				}
			}
		}
		
		else if ($cmd == 'deleteDomain') {
			if (!($domain = param($input, 1))) {
				println("Syntax: deleteDomain <domain>[@<system>]");
			}
			else {
				if (preg_match("/([^@]+)@(.*)/", $domain, $matches)) {
					$domain = $matches[1];
					$system = $matches[2];	
				}
				else
					$system = 'default';
			
				try {
					slData::deleteDomain($domain, $system);		
				} catch (Exception $e) {
					println($e->getMessage());
				}
			}
		}
		
		else if ($cmd == 'save') {
			if (!($itemName = param($input, 1)) || !($domain = param($input, 2))) {
				println("Syntax: save <itemName> <domain>[@<system>]");
			}
			else {
				if (preg_match("/([^@]+)@(.*)/", $domain, $matches)) {
					$domain = $matches[1];
					$system = $matches[2];	
				}
				else
					$system = 'default';
					
				$session['packet'] = new slData($domain, $itemName, $system);
				$next_property = 'save.property';
			}
		}
		
		else if ($cmd == 'update') {
			if (!($itemName = param($input, 1)) || !($domain = param($input, 2))) {
				println("Syntax: update <itemName> <domain>[@<system>]");
			}
			else {
				if (preg_match("/([^@]+)@(.*)/", $domain, $matches)) {
					$domain = $matches[1];
					$system = $matches[2];	
				}
				else
					$system = 'default';
					
				$session['packet'] = new slData($domain, $itemName, $system);
				$next_property = 'update.property';
			}
		}
		
		else if ($cmd == 'systems') {
			foreach (slData::getSystems() as $name => $system) {
				if ($system['type'] == 'simpledb')
					println();
				else
					println("$name: $system[type] on $system[host] ($system[user])");
			}
		}
		
		else if ($cmd == 'val') {
			if (!($query = param($input, 1, true))) {
				println("You didn't ask me to query anything.");
			}
			else {
				try {
					println(val($query));
				} catch (Exception $e) {
					println($e->getMessage());	
				}
			}
		}
		
		else {
			println("Huh? Sorry. I don't understand.");	
		}
		
	}
	
	else if ($value = chomp($input)) {
		
		if ($next_property == 'connect.host') {
			$session[$next_property] = $value;
			$next_property = 'connect.user';
		}
		
		else if ($next_property == 'connect.user') {
			$session[$next_property] = $value;
			$next_property = 'connect.pass';
		}
		
		else if ($next_property == 'connect.pass') {
			$session[$next_property] = $value;
			$next_property = 'connect.database';
		}
		
		else if ($next_property == 'connect.database') {
			$session[$next_property] = $value;
			try {
				connect(
					$session['connect.host'],
					$session['connect.user'],
					$session['connect.pass'],
					$session['connect.database']
				);
				println("Connected!");
			} catch (Exception $e) {
				println($e->getMessage());	
			}
			$next_property = null;
		}	
		
		
		else if ($next_property == 'save.property') {
			$session[$next_property] = trim($value);
			$next_property = 'save.value';	
		}
		
		else if ($next_property == 'save.value') {
			$prop = $session['save.property'];
			$packet = $session['packet'];
			try {
				$packet->$prop = slvalue($value);
				$next_property = 'save.property';	
			} catch (Exception $e) {
				echo $e->getMessage();
				$next_property = null;
			}
		}
		
		else if ($next_property == 'update.property') {
			$session[$next_property] = trim($value);
			$next_property = 'update.value';	
		}
		
		else if ($next_property == 'update.value') {
			$prop = $session['update.property'];
			$packet = $session['packet'];
			try {
				$packet->$prop = slvalue($value);
				$next_property = 'update.property';	
			} catch (Exception $e) {
				echo $e->getMessage();
				$next_property = null;
			}
		}
		
	}
	
	else { // empty line entry

		if ($next_property == 'save.property') {
			$packet = $session['packet'];
			try {
				$packet->save();
				println('Saved!');
			}
			catch (Exception $e) {
				println($e->getMessage());
			}
			$next_property = null;
			$session['packet'] = null;
		}
		
		else if ($next_property == 'save.value') {
			$prop = $session['save.property'];
			$packet = $session['packet'];
			try {
				$packet->$prop = null;
				$next_property = 'save.property';	
			} catch (Exception $e) {
				echo $e->getMessage();
				$next_property = null;
			}
		}
		
		else if ($next_property == 'update.property') {
			$packet = $session['packet'];
			try {
				$packet->update();
				println('Updated!');
			}
			catch (Exception $e) {
				println($e->getMessage());
			}
			$next_property = null;
			$session['packet'] = null;
		}
		
		else if ($next_property == 'update.value') {
			$prop = $session['update.property'];
			$packet = $session['packet'];
			try {
				$packet->$prop = null;
				$next_property = 'update.property';	
			} catch (Exception $e) {
				echo $e->getMessage();
				$next_property = null;
			}
		}
		
	}
	
	
	echo "scottlib> ".($next_property ? "$next_property: " : "");
	
	
} while ($input = fgets(STDIN));

